<?php

/**
 *  @FileComment            [后台用户管理控制器]
 *  @ProjectName             项目名称：JDshop
 *  @FileName                   文件名称：AdminController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-25 22:32:00
 *  *
 */
class AdminController extends CommonController {

        private $model;

        public function __init() {
                parent::__init();
                $this->model = K('Admin');
        }

        public function index() {
                $data = $this->model->get_data();
                $this->assign('data', $data);
                $this->display();
        }

        public function add() {
                if (IS_POST) {
                        if ($this->model->create()) {
                                $this->model->add_user();
                                $this->success('用户添加成功！', U('index'));
                        } else {
                                $this->error($this->model->error);
                        }
                }
                $this->display();
        }

        public function del() {
                $aid = Q('get.aid', 0, 'intval');
                if ($this->model->del_data($aid)) {
                        $this->success('删除用户成功！');
                } else {
                        $this->error('请先删除该用户的所有角色！');
                }
        }

        public function role() {
                $data = K('Role')->get_data();
                $this->assign('data', $data);
                $this->display();
        }

        public function addRole() {
                $role = K('Role');
                if (IS_POST) {
                        if ($role->create()) {
                                $role->add_data();
                                $this->success('添加角色成功！', U("index"));
                        } else {
                                $this->error($role->error);
                        }
                }
                $this->display();
        }

        public function delRole() {
                $rid = Q('get.rid', 0, 'intval');
                K('Role')->del_data($rid);
                $this->success('删除角色成功！');
        }

        public function RoleList() {
                $aid = Q('get.aid', 0, 'intval');
                $data = K('AdminRoleListView')->get_data('admin_role.aid=' . $aid);
                $this->assign('data', $data);
                $this->display();
        }

        public function addAdminRole() {
                $adminRole = K('AdminRole');
                if (IS_POST) {
                        $aid = Q("post.aid", 0, 'intval');
                        $rid = Q("post.rid", 0, 'intval');
                        $data = $adminRole->get_one_data($aid, $rid);
                        if (!empty($data)) {
                                $this->error('该角色无需重复添加！', U('Admin/RoleList', array('aid' => $aid)));
                        }
                        $adminRole->add_data();
                        $this->success('添加成功！', U('Admin/RoleList', array('aid' => $aid)));
                }
                $aid = Q("get.aid", 0, 'intval');
                $userInfo = $this->model->get_one_data(array('aid' => $aid));
                $role = K('Role')->get_data();
                $this->assign('role', $role);
                $this->assign('userInfo', $userInfo);
                $this->display();
        }

        public function delAdminRole() {
                $aid = Q("get.aid", 0, 'intval');
                $rid = Q("get.rid", 0, 'intval');
                K('AdminRole')->del_data($aid, $rid);
                $this->success('角色删除成功！');
        }

}
